package cn.itcast.item.service.impl;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageDTO<Item> getPage(SearchItemDTO dto) {
        //构建分页条件
        Integer current=null;
        if(ObjectUtils.isNotEmpty(dto.getPage())) current = dto.getPage();

        Integer size=null;
        if(ObjectUtils.isNotEmpty(dto.getSize()))
        size = dto.getSize();
        IPage<Item> itemPage=null;
        if(ObjectUtils.isEmpty(dto.getPage())||ObjectUtils.isEmpty(dto.getSize()))
            itemPage = new Page();else itemPage = new Page(current,size);

        //构建查询条件

        String name = dto.getName();
        Date dateStart = dto.getBeginDate();
        Date dateEnd = dto.getEndDate();
        String brand = dto.getBrand();
        String category = dto.getCategory();

        QueryWrapper<Item> wrappers = new QueryWrapper<Item>()
                .and(StringUtils.isNotBlank(name), wrapper -> wrapper.like("name", name))
                .and(StringUtils.isNotBlank(category), wrapper -> wrapper.eq("category", category))
                .and(!ObjectUtils.isEmpty(dateStart), wraper -> wraper.ge("create_time", dateStart))
                .and(!ObjectUtils.isEmpty(dateEnd), wraper -> wraper.le("create_time", dateEnd))
                .and(StringUtils.isNotBlank(brand), wrapper -> wrapper.eq("brand", brand));

        //查询
        this.itemMapper.selectPage(itemPage, wrappers);
        List<Item> records = itemPage.getRecords();
        if (records.equals(null) || itemPage.getTotal() == 0)
            return null;
        else {
            Long size1 = itemPage.getTotal();
            PageDTO pageDTO = new PageDTO(size1, records);
//            System.out.println(records.size());
            return pageDTO;
        }
    }

    @Override
    public ResultDTO upAndDown(Long id, Integer status) {
        //先把商品查出来
        Item item = itemMapper.selectById(id);
        item.setStatus(status);
        int i = itemMapper.updateById(item);
        return i == 1 ? ResultDTO.ok("操作成功") : ResultDTO.error("操作失败");
    }

    @Override
    public ResultDTO deleteItemById(Long id) {
        //先把商品查出来
        Item item = itemMapper.selectById(id);
        //判断有没有下架
        Integer status1 = item.getStatus();
        if (ObjectUtils.isNotEmpty(item) && status1 == 1) return ResultDTO.error("商品还未下架，无法删除");
        else {
            int i = itemMapper.deleteById(id);
            return i == 0 ? ResultDTO.error("删除失败") : ResultDTO.ok("删除成功");
        }
    }

    @Override
    public Item getItemById(Long id) {
        Item item = itemMapper.selectMy(id);
        System.out.println(item);
        return item;
    }
}
